﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Alserve.Ffl.Data;
using Alserve.Ffl;

public partial class ffl_admin_ManagerOfMonth : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!Security.IsAdmin(Page))
                Response.Redirect("~//ffl//anonymous//default.aspx");

            PopulateMonthDropDown();
            PopulateTeamDropDown();
            ConnectManagerOfMonthRepeater();
        }
    }

    private void PopulateMonthDropDown()
    {
        DateTime startDate = DateTime.Today;

        while (startDate.Month != 8)
            startDate = startDate.AddMonths(-1);

        for (Int32 monthCount = 1; monthCount <= 10; monthCount++)
        {
            ListItem newItem = new ListItem(startDate.ToString("MMMM") + " " + startDate.Year.ToString(), startDate.Month.ToString());
            MonthDropDown.Items.Add(newItem);
            startDate = startDate.AddMonths(1);
        }
    }

    private void PopulateTeamDropDown()
    {
        Alserve.Ffl.Data.User thisUser = new Alserve.Ffl.Data.User();
        thisUser.Where.Paid.Value = true;
        thisUser.Query.AddOrderBy(Alserve.Ffl.Data.User.ColumnNames.Surname, MyGeneration.dOOdads.WhereParameter.Dir.ASC);
        thisUser.Query.AddOrderBy(Alserve.Ffl.Data.User.ColumnNames.Forename, MyGeneration.dOOdads.WhereParameter.Dir.ASC);
        if (thisUser.Query.Load())
        {
            while (!thisUser.EOF)
            {
                ListItem newItem = new ListItem(thisUser.Forename + " " + thisUser.Surname + " (" + thisUser.TeamName + ")", thisUser.Id.ToString());
                TeamDropDown.Items.Add(newItem);
                thisUser.MoveNext();
            }
        }
    }

    private void ConnectManagerOfMonthRepeater()
    {
        ManagerOfMonthRepeater.DataSource = null;
        _ManagerOfMonthView thisMom = new _ManagerOfMonthView();
        if(thisMom.LoadAll())
            ManagerOfMonthRepeater.DataSource = thisMom.DefaultView;
        ManagerOfMonthRepeater.DataBind();
        
    }

    protected void AddButton_Click(object sender, EventArgs e)
    {
        ManagerOfMonth thisMom = new ManagerOfMonth();
        thisMom.AddNew();
        thisMom.UserId = Convert.ToInt32(TeamDropDown.SelectedValue);
        thisMom.MonthNumber = Convert.ToInt32(MonthDropDown.SelectedValue);
        thisMom.Points = Convert.ToInt32(PointsTextBox.Text.Trim());
        thisMom.Save();
        ConnectManagerOfMonthRepeater();
    }

    protected void ManagerOfMonthRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Int32 managerOfMonthId = Convert.ToInt32(((HiddenField)e.Item.FindControl("IdHidden")).Value);
        ManagerOfMonth thisMom = new ManagerOfMonth();
        thisMom.Where.Id.Value = managerOfMonthId;
        if (thisMom.Query.Load())
        {
            thisMom.MarkAsDeleted();
            thisMom.Save();
            ConnectManagerOfMonthRepeater();
        }
    }
}
